import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const username = ref<string | null>(localStorage.getItem('username'))

    function login(name: string) {
      username.value = name
      localStorage.setItem('username', name)
    }

    function logout() {
      username.value = null
      localStorage.removeItem('username')
    }

    return { username, login, logout }
  },
  {
    persist: true,
  },
)
